package delegate;

import java.util.List;

import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import dtos.ArticuloDTO;
import dtos.ItemVentaDTO;
import dtos.LogDTO;
import dtos.VentaDTO;
import entities.Articulo;
import entities.ConfiguracionLog;
import entities.ItemVenta;
import interfaces.FacadeStateful;
import interfaces.FacadeStateless;

public class BusinessDelegate {

	private static BusinessDelegate instancia;
	
	@EJB
	private  FacadeStateless facadeLess;
	
	@EJB
	private FacadeStateful facadeFul;
	
	
	
	public static BusinessDelegate getInstancia() throws NamingException 
	{
		if ( instancia == null )
		{
			instancia = new BusinessDelegate();
		}
		return instancia;
	}
	
	public String login(String usr, String pass){
		String estado = facadeLess.login(usr, pass);
		return estado;
		
	}
	private BusinessDelegate() throws NamingException 
	{
        final String viewClassName = FacadeStateless.class.getName();
        final String viewClassNameFul = FacadeStateful.class.getName();
        InitialContext initialContext = new InitialContext();
		facadeLess = (FacadeStateless)	initialContext.lookup("java:global/PortalWebComprasEAR/PortalWebCompras/FacadeStatelessBean"+"!"+viewClassName);
		facadeFul = (FacadeStateful) initialContext.lookup("java:global/PortalWebComprasEAR/PortalWebCompras/FacadeStatefulBean"+"!"+viewClassNameFul);
	}
	
	public void registrarArticulo ( Articulo a )
	{
		facadeLess.registrarArticulo(a);
	}
	
	public List<ArticuloDTO> obtenerArticulos () {
		return facadeLess.obtenerArticulos();
	}
	 
	public double agregarItemAlCarrito ( int codigo, int cantidad ) {
		return facadeFul.agregarItemAlCarrito( codigo, cantidad );
	}
	
	public VentaDTO confirmarVenta () {
		VentaDTO dto = facadeFul.confirmarCompra();
		return dto;
	}

	public void eliminarItemCarrito(int codigo) {
		facadeFul.eliminarItemCarrito(codigo);
	}

	public boolean iniciarVenta(String usr) {
		return facadeFul.iniciarVenta(usr);
	}
	
	public void registrarRanking(List<ArticuloDTO> articulosDTO) {
		facadeLess.registrarRanking(articulosDTO);
	}
	
	public List<VentaDTO> obtenerVentas(String login) {
		return facadeLess.obtenerVentas(login);
	}
	
	public List<ArticuloDTO> obtenerPorOrden(String tipo, String columna,String orden){
		return facadeLess.obtenerArticulosPorOrden(tipo, columna,orden);
	}
	
	public  String toJavascriptArray(List<String> lista){
		return facadeLess.toJavascriptArray(lista);
	}
	
	public void enviarLogSegunConfig(String mensaje) {
		facadeLess.enviarLogSegunConfig( mensaje );	
	} 

	public void establecerConfiguracionLog(Integer idGrupo, String urlDestino, String configuracion, String ip, String puerto) {
		facadeLess.establecerConfiguracionLog(idGrupo, urlDestino, configuracion, ip, puerto);
	}
	
	 public ConfiguracionLog obtenerConfiguracionActualLog() {
		 return facadeLess.obtenerConfiguracionActualLog();
	 }

	public void cancelar() {
		facadeFul.cancelarVenta();
	}

	public List<ItemVenta> obtenerItemsCarrito() {
		return facadeFul.obtenerItemsCarrito();
	}

	public List<ArticuloDTO> busqueda(String valor, String tipo) {
		return facadeLess.busqueda (valor,tipo);
	}

}
